package com.heihei.combine.applist;

import com.heihei.config.ConfigProperties;
import com.heihei.util.FileUtilF;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *  gclone 脚本生成
 *
 * @version 1.0
 * @since JDK1.8
 * @author duanhengfei
 * @date 2021年02月01日 19:48:42
 */
public class GcloneGenerator{
    public static void main(String[] args) throws IOException {
//        startBeiFen();
        startKuaiSuZhuanCun();
    }

    public static void startZhuanCun() {
        System.out.println("请输入源文件目录（txt文件）");

    }

    /**
     * 快速转存
     *
     * @param
     * @Return
     * @throws
     * @author duanhengfei
     * @date 2021年02月01日 22:27:57
     */
    public static void startKuaiSuZhuanCun() throws IOException {
        System.out.println("请输入源文件目录（txt文件）");
        File validSrcFile = FileUtilF.getValidSrcFile();
        List<String> preList = FileUtilF.readTxtFileByLine(new File(validSrcFile.getParentFile(), ConfigProperties.PRE_FILENAME));
        //逐行读取文件
        List<String> needZhuanCunList = FileUtilF.readTxtFileByLine(validSrcFile);
        //拼接转存脚本字符串
        List<List<String>> zhuanCunCommandList = buildZhuanCunCommand(needZhuanCunList, preList);
        //写文件
        writeFile(zhuanCunCommandList, validSrcFile.getParentFile());
        System.out.println("OK √");
    }

    /**
     * 写命令
     *
     * @param zhuanCunCommandList
     * @param parentFile
     * @Return
     * @throws
     * @author duanhengfei
     * @date 2021年02月01日 22:32:13
     */
    private static void writeFile(List<List<String>> zhuanCunCommandList, File parentFile) throws IOException {
        int index = 1;
        File zhuanCunDir = getZhuanCunDir(parentFile);
        if(!zhuanCunDir.exists()) {
            zhuanCunDir.mkdirs();
        }
        for (List<String> zhuanCunList : zhuanCunCommandList) {
            FileUtilF.writeTxtFileByLine(new File(zhuanCunDir, index++ + ".cmd"), zhuanCunList, "GBK");
        }
    }

    private static File getZhuanCunDir(File parentFile) {
        int index = 1;
        File file = new File(parentFile, "转存");
        while(file.exists() && file.isDirectory()) {
            file = new File(parentFile, "转存" + index++);
        }
        return file;
    }

    /**
     * 拼接转存命令
     *
     *
     * @param zhuanCunList
     * @param preList
     * @Return {@link List< List< String>>}
     * @throws
     * @author duanhengfei
     * @date 2021年02月01日 22:31:24
     */
    private static List<List<String>> buildZhuanCunCommand(List<String> zhuanCunList, List<String> preList) {
        List<List<String>> resultList = new ArrayList<>();
        for (String s : zhuanCunList) {
            List<String> list = new ArrayList<>();
            list.addAll(preList);
            list.add("");
            String[] split = s.split(ConfigProperties.SEPARATOR);
            String command = "gclone sync \"" + ConfigProperties.ZHUAN_CUN_DRIVE + ":{" + split[0] + "}\" \"" + ConfigProperties.ZHUAN_CUN_DES + split[1] + "/\" --drive-server-side-across-configs -v";
            list.add(command);
            list.add("pause");
            list.add("pause");
            resultList.add(list);
        }
        return resultList;
    }

    /**
     * 开始生成脚本
     *
     * @param
     * @Return
     * @throws
     * @author duanhengfei
     * @date 2021年02月01日 19:50:48
     */
    public static void startBeiFen() throws IOException {
        System.out.println("请输入源文件目录（txt文件）");
        File validSrcFile = FileUtilF.getValidSrcFile();
        //逐行读取文件
        List<String> needCopyDirList = FileUtilF.readTxtFileByLine(validSrcFile);
        //拼接字符串
        List<String> resultStrList = getResultStrList(needCopyDirList);
        //前缀
        List<String> preList = FileUtilF.readTxtFileByLine(new File(validSrcFile.getParentFile(), ConfigProperties.PRE_FILENAME));
        preList.addAll(resultStrList);
        //逐行写文件
        File targetTxtFile = getTargetTxtFile(validSrcFile);
        FileUtilF.writeTxtFileByLine(targetTxtFile, preList, "GBK");
        System.out.println("OK √");
    }

    /**
     * 拼接脚本字符串
     *
     * @param needCopyDirList
     * @Return {@link List< String>}
     * @throws
     * @author duanhengfei
     * @date 2021年02月01日 20:15:32
     */
    private static List<String> getResultStrList(List<String> needCopyDirList) {
        List<String> resultStrList = new ArrayList<>();
        resultStrList.add("");
        for (int i = 0; i < ConfigProperties.DRIVE_NAME_ARR.length - 1; i++) {
            for (String s : needCopyDirList) {
                String currLineStr = "gclone sync \"" + ConfigProperties.DRIVE_NAME_ARR[i] + ":" + s + "\" \"" + ConfigProperties.DRIVE_NAME_ARR[i+1] + ":" + s + "\" --drive-server-side-across-configs -v";
                resultStrList.add(currLineStr);
            }
            resultStrList.add("");
            for (String s : needCopyDirList) {
                String currLineStr = "gclone sync \"" + ConfigProperties.DRIVE_NAME_ARR[i] + ":" + s + "\" \"" + ConfigProperties.DRIVE_NAME_ARR[i+1] + ":" + s + "\" --drive-server-side-across-configs -v";
                resultStrList.add(currLineStr);
            }
            resultStrList.add("");
            resultStrList.add("");
        }
        resultStrList.add("pause");
        resultStrList.add("pause");
        return resultStrList;
    }

    /**
     * 获取目标脚本
     *
     * @param validSrcFile
     * @Return {@link File}
     * @throws
     * @author duanhengfei
     * @date 2021年02月01日 20:08:08
     */
    private static File getTargetTxtFile(File validSrcFile) {
        File parentFile = validSrcFile.getParentFile();
        File resultFile = new File(parentFile, validSrcFile.getName() + "_中转.cmd");
        int index = 1;
        while(resultFile.exists()) {
            resultFile = new File(parentFile, validSrcFile.getName() + "_中转" + index++ + ".cmd");
        }
        return resultFile;
    }
}
